home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1951 / 1951.xpi / chrome / fission.jar / content / fission / fission.js < prev    next >
Text File  |  2009-04-28  |  9KB  |  300 lines

  1. const Fission = {
  2.     mPrefBranch: Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.fission.").QueryInterface(Components.interfaces.nsIPrefBranch2),
  3.  
  4. /* ........ (Un)initialization .............. */
  5.  
  6.     onLoad: function()
  7.     {
  8.         if (!window.gProgressMeterPanel)
  9.         {
  10.             setTimeout(function() { Fission.onLoad(); }, 100);
  11.             return;
  12.         }
  13.         this.init();
  14.         
  15.         document.getElementById("cmd_CustomizeToolbars").addEventListener("DOMAttrModified", this.onDOMAttrModified_command, false);
  16.         getBrowser().tabContainer.addEventListener("TabSelect", this.onTabSelect_tabContainer, false);
  17.         this.mPrefBranch.addObserver("", this, false);
  18.         
  19.         if (/onProgressChange\([^,]+, 0/.test(XULBrowserWindow.onUpdateCurrentBrowser))
  20.         { /* fix for Mozilla bug 477261 */
  21.             eval("XULBrowserWindow.onUpdateCurrentBrowser = " + XULBrowserWindow.onUpdateCurrentBrowser.toString().replace(/onProgressChange\(.*?,/, "$& null, ").replace("if (loadingDone)", "if (gProgressCollapseTimer) { gProgressMeterPanel.collapsed = true; clearTimeout(gProgressCollapseTimer); gProgressCollapseTimer = null; } $&"));
  22.             eval("gBrowser.updateCurrentBrowser = " + gBrowser.updateCurrentBrowser.toString().replace("p.onStateChange(", 'if (!("onUpdateCurrentBrowser" in p)) $&'));
  23.         }
  24.     },
  25.  
  26.     onUnload: function()
  27.     {
  28.         document.getElementById("cmd_CustomizeToolbars").removeEventListener("DOMAttrModified", this.onDOMAttrModified_command, false);
  29.         gBrowser.tabContainer.removeEventListener("TabSelect", this.onTabSelect_tabContainer, false);
  30.         this.mPrefBranch.removeObserver("", this);
  31.         
  32.         this.uninit();
  33.         
  34.         this.mProgressPanel = null;
  35.         this.mProgressBar = null;
  36.         this.mText = null;
  37.     },
  38.  
  39.     init: function()
  40.     {
  41.         if (!this.mProgressPanel)
  42.         {
  43.             this.mProgressPanel = gProgressMeterPanel;
  44.         }
  45.         if (!gURLBar && !((gURLBar = document.getElementById("urlbar"))))
  46.         {
  47.             return;
  48.         }
  49.         this.mProgressBar = document.getElementById("statusbar-icon");
  50.         
  51.         let iconize = this.mPrefBranch.getBoolPref("iconize");
  52.         if (iconize)
  53.         {
  54.             let iconBox = document.getElementById("urlbar-icons") || document.getElementById("lock-icon").parentNode;
  55.             iconBox.insertBefore(this.mProgressBar, iconBox.firstChild);
  56.         }
  57.         else
  58.         {
  59.             gURLBar.appendChild(this.mProgressBar);
  60.         }
  61.         if (this.mPrefBranch.getBoolPref("textify"))
  62.         {
  63.             this.textify();
  64.         }
  65.         if (this.mPrefBranch.getBoolPref("linkify"))
  66.         {
  67.             this.linkify();
  68.         }
  69.         this.updateBackgroundColor();
  70.         
  71.         gProgressMeterPanel.hidden = true;
  72.         gProgressMeterPanel = this;
  73.         
  74.         gURLBar.setAttribute("fission", iconize ? "icon" : "fusion");
  75.         gURLBar.addEventListener("ValueChange", this.onValueChange_urlbar, true);
  76.         if (this.mLinkified || this.mText && this.mText.parentNode)
  77.         {
  78.             gURLBar.addEventListener("focus", this.onFocusBlur_urlbar, true);
  79.             gURLBar.addEventListener("blur", this.onFocusBlur_urlbar, true);
  80.             delete this.mPendingOverLink;
  81.         }
  82.         
  83.         if (Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch).getCharPref("general.skins.selectedSkin") == "classic/1.0")
  84.         {
  85.             let appInfo = Components.classes["@mozilla.org/xre/app-info;1"].getService(Components.interfaces.nsIXULAppInfo).QueryInterface(Components.interfaces.nsIXULRuntime);
  86.             gURLBar.setAttribute("fission-skin", appInfo.OS + appInfo.version);
  87.         }
  88.         
  89.         this.mLoaded = true;
  90.     },
  91.  
  92.     textify: function()
  93.     {
  94.         if (!this.mText)
  95.         {
  96.             this.mText = document.createElement("label");
  97.             this.mText.setAttribute("id", "fission-status");
  98.             this.mText.setAttribute("crop", "right");
  99.             
  100.             eval("XULBrowserWindow.updateStatusField = " + XULBrowserWindow.updateStatusField.toString().replace(/text =[^;]+;/, "$& if (Fission.updateStatusField(this.status || this.defaultStatus)) text = this.overLink || this.jsStatus || this.jsDefaultStatus;"));
  101.         }
  102.         let iconBox = document.getElementById("urlbar-icons") || document.getElementById("lock-icon").parentNode;
  103.         iconBox.insertBefore(this.mText, iconBox.firstChild);
  104.     },
  105.  
  106.     linkify: function()
  107.     {
  108.         if (!/Fission/.test(XULBrowserWindow.setOverLink))
  109.         {
  110.             eval("XULBrowserWindow.setOverLink = " + XULBrowserWindow.setOverLink.toString().replace(/{/, "$& link = Fission.setOverLink(link);"));
  111.             if (window.focusAndSelectUrlBar)
  112.             {
  113.                 eval("focusAndSelectUrlBar = " + focusAndSelectUrlBar.toString().replace("gURLBar.focus();", 'if (!gURLBar.hasAttribute("focused")) $&'));
  114.             }
  115.             eval("openLocation = " + openLocation.toString().replace("gURLBar.focus();", 'if (!gURLBar.hasAttribute("focused")) $&')); // prevent distracting focus/blur events
  116.         }
  117.         this.mLinkified = true;
  118.     },
  119.  
  120.     uninit: function()
  121.     {
  122.         if (!this.mLoaded)
  123.         {
  124.             return;
  125.         }
  126.         
  127.         this.updateBackgroundColor(true);
  128.         
  129.         gProgressMeterPanel = this.mProgressPanel;
  130.         gProgressMeterPanel.appendChild(this.mProgressBar);
  131.         gProgressMeterPanel.hidden = false;
  132.         
  133.         gURLBar.setAttribute("fission", "");
  134.         gURLBar.removeEventListener("ValueChange", this.onValueChange_urlbar, true);
  135.         if (this.mLinkified || this.mText && this.mText.parentNode)
  136.         {
  137.             gURLBar.removeEventListener("focus", this.onFocusBlur_urlbar, true);
  138.             gURLBar.removeEventListener("blur", this.onFocusBlur_urlbar, true);
  139.         }
  140.         
  141.         if (this.mText && this.mText.parentNode)
  142.         {
  143.             this.mText.parentNode.removeChild(this.mText);
  144.         }
  145.         this.mLinkified = false;
  146.         
  147.         this.mLoaded = false;
  148.     },
  149.  
  150. /* ........ State Change Observers .............. */
  151.  
  152.     set collapsed(aValue)
  153.     {
  154.         if (aValue && this.mProgressBar.value != 100)
  155.         {
  156.             this.mProgressBar.value = 100;
  157.         }
  158.     },
  159.  
  160.     onValueChange_urlbar: function(aEvent)
  161.     {
  162.         if (aEvent.originalTarget == Fission.mProgressBar)
  163.         {
  164.             this.setAttribute("progress", Fission.mProgressBar.value);
  165.         }
  166.     },
  167.  
  168.     onFocusBlur_urlbar: function(aEvent)
  169.     {
  170.         if (this.hasAttribute("fission-link"))
  171.         {
  172.             this[aEvent.type == "focus" ? "addEventListener" : "removeEventListener"]("keyup", Fission.onKeyUp_urlbar, false);
  173.         }
  174.         if (!(Fission.mURLBar_focused = aEvent.type == "focus") && "mPendingOverLink" in Fission)
  175.         {
  176.             Fission.setOverLink(Fission.mPendingOverLink);
  177.             delete Fission.mPendingOverLink;
  178.         }
  179.         if (Fission.mText)
  180.         {
  181.             Fission.mText.hidden = Fission.mURLBar_focused;
  182.         }
  183.     },
  184.  
  185.     onKeyUp_urlbar: function(aEvent)
  186.     {
  187.         if (this.hasAttribute("fission-link") && this.value != this.getAttribute("fission-link"))
  188.         {
  189.             this.removeAttribute("fission-link");
  190.             this.removeEventListener("keyup", arguments.callee, false);
  191.         }
  192.     },
  193.  
  194.     onDOMAttrModified_command: function(aEvent)
  195.     {
  196.         if (aEvent.attrName == "disabled")
  197.         {
  198.             Fission[aEvent.newValue != "true" ? "init" : "uninit"]();
  199.         }
  200.     },
  201.  
  202.     onTabSelect_tabContainer: function(aEvent)
  203.     {
  204.         if (Fission.mProgressBar.endAnimation)
  205.         {
  206.             // prevent animation when changing tabs
  207.             let tabListener = gBrowser.mTabListeners[aEvent.originalTarget._tPos] || null;
  208.             let tabProgress = !tabListener || (tabListener.mStateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP) ? 100 : Math.round(tabListener.mTotalProgress * 100);
  209.             Fission.mProgressBar.endAnimation(tabProgress);
  210.         }
  211.     },
  212.  
  213.     observe: function(aSubject, aTopic, aData)
  214.     {
  215.         if (aTopic == "nsPref:changed" && this.mLoaded)
  216.         {
  217.             switch (aData)
  218.             {
  219.             case "color":
  220.                 this.updateBackgroundColor();
  221.                 break;
  222.             case "iconize":
  223.             case "textify":
  224.             case "linkify":
  225.                 setTimeout(function() { Fission.uninit(); Fission.init(); }, 0);
  226.                 break;
  227.             }
  228.         }
  229.     },
  230.  
  231. /* ........ Auxiliary Functions .............. */
  232.  
  233.     updateStatusField: function(aString)
  234.     {
  235.         if (!this.mText || !this.mText.parentNode)
  236.         {
  237.             return false;
  238.         }
  239.         
  240.         aString = aString != gNavigatorBundle.getString("nv_done") ? aString : "";
  241.         if (this.mText.value != aString)
  242.         {
  243.             this.mText.value = aString;
  244.         }
  245.         
  246.         return true;
  247.     },
  248.  
  249.     setOverLink: function(aLink)
  250.     {
  251.         if (this.mURLBar_focused && (!document.commandDispatcher.focusedElement || document.commandDispatcher.focusedElement == gURLBar))
  252.         { /* ugly hack for nasty focus issue - see Mozilla bug 408060 */
  253.             content.focus(); gURLBar.focus(); content.focus();
  254.         }
  255.         
  256.         if (this.mURLBar_focused)
  257.         {
  258.             this.mPendingOverLink = aLink;
  259.             return this.mLinkified ? "" : aLink;
  260.         }
  261.         
  262.         if (this.mLinkified && aLink)
  263.         {
  264.             if (!this.mURLBar_value)
  265.             {
  266.                 this.mURLBar_value = "@" + gURLBar.value;
  267.             }
  268.             if (gURLBar.getAttribute("fission-link") != aLink)
  269.             {
  270.                 gURLBar.setAttribute("fission-link", (gURLBar.value = aLink));
  271.             }
  272.             return "";
  273.         }
  274.         
  275.         if (this.mURLBar_value)
  276.         {
  277.             if (gURLBar.hasAttribute("fission-link") && gURLBar.value == gURLBar.getAttribute("fission-link"))
  278.             {
  279.                 gURLBar.value = this.mURLBar_value.substr(1);
  280.             }
  281.             gURLBar.removeAttribute("fission-link");
  282.             delete this.mURLBar_value;
  283.         }
  284.         
  285.         return aLink;
  286.     },
  287.  
  288.     updateBackgroundColor: function(aReset)
  289.     {
  290.         let chunk = document.getAnonymousElementByAttribute(this.mProgressBar, "class", "progress-bar");
  291.         if (chunk)
  292.         {
  293.             chunk.style.background = !aReset ? this.mPrefBranch.getCharPref("color").replace(/^(url\(.+\)) #[0-9A-F]{6}$/, "$1 repeat-x left center transparent") : "";
  294.         }
  295.     }
  296. };
  297.  
  298. window.addEventListener("load", function() { Fission.onLoad(); }, false);
  299. window.addEventListener("unload", function() { Fission.onUnload(); }, false);
  300.